#include "Utility.h"

using namespace std;

namespace Utility {
    vector <RooDataHist *> createRooDataHistVector(string prefix, vector <TH1D *> histVector, RooRealVar & isol, GarbageCollector * gc) {
        vector <RooDataHist *> res;
        for(int i = 0;i<(int)histVector.size();i++) {
            string name = prefix+"DataHistogram"+boost::lexical_cast<string>(i);
            RooDataHist * dataHist = new RooDataHist(name.c_str(),name.c_str(),isol,histVector[i]);
            gc->add(dataHist);
            res.push_back(dataHist);
        }
        return res;
    }
    vector <RooHistPdf *> createRooHistPdfVector(string prefix, vector <RooDataHist *> dataHistVector, RooRealVar & isol, GarbageCollector * gc) {
        vector <RooHistPdf *> res;
        for(int i = 0;i<(int)dataHistVector.size();i++) {
            string name = prefix+"HistPdf"+boost::lexical_cast<string>(i);
            RooHistPdf * histPdf = new RooHistPdf(name.c_str(),name.c_str(),isol,*dataHistVector[i]);
            gc->add(histPdf);
            res.push_back(histPdf);
        }
        return res;
    }
    vector <RooRealVar *> createRooRealVarVector(string prefix, int il, double val, double low, double high, GarbageCollector * gc) {
        vector <RooRealVar *> res;
        for(int i = 0;i<il;i++) {
            string name = prefix+""+boost::lexical_cast<string>(i);
            RooRealVar * realVar = new RooRealVar(name.c_str(),name.c_str(),val,low,high);
            gc->add(realVar);
            res.push_back(realVar);
        }
        return res;
    }
    RooAddPdf * createRooPdfAsSum(string name, vector <RooHistPdf *> sigHistPdfVector, vector <RooHistPdf *> bgdHistPdfVector, vector <RooRealVar *> sigVars, vector <RooRealVar *> bgdVars, GarbageCollector * gc) {
        if(sigHistPdfVector.size()!=sigVars.size() || bgdHistPdfVector.size()!=bgdVars.size()) throw UtilityException(("Mismatched vector sizes in line: "+boost::lexical_cast<string>(__LINE__)).c_str());
        RooArgList pdfList, coefList;
        for(int i = 0;i<(int)sigHistPdfVector.size();i++) 
            pdfList.add(*sigHistPdfVector[i]);
        for(int i = 0;i<(int)bgdHistPdfVector.size();i++) 
            pdfList.add(*bgdHistPdfVector[i]);
        for(int i = 0;i<(int)sigVars.size();i++) 
            coefList.add(*sigVars[i]);
        for(int i = 0;i<(int)bgdVars.size();i++) 
            coefList.add(*bgdVars[i]);
        RooAddPdf * model = new RooAddPdf(name.c_str(),name.c_str(),pdfList,coefList);
        gc->add(model);
        return model;
    }
    void addToStats(TPaveText & stats, RooRealVar & var) {
        stats.AddText(TString::Format(TString::Format("%s = %5.0f +/- %5.1f", var.GetName(), var.getVal(), var.getError())));
    }
    void addStartingValueToStats(TPaveText & stats, RooRealVar & var) {
        stats.AddText(TString::Format("%s  = %5.0f [%5.0f,%5.0f]", var.GetName(), var.getVal(), var.getMin(), var.getMax()));
    }

}
